//
//  ViewController.m
//  数据存储
//
//  Created by Clement_Gu on 16/3/14.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "ViewController.h"
#import "StudentModel.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    NSString  *path = NSHomeDirectory();
//    NSLog(@"%@",path);
////    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
////   
////    label.text = path;
////    [self.view addSubview:label];
////
//    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    UILabel *labelID = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 50, 30)];
//    labelID.text = @"学号";
//    [self.view addSubview:labelID];
//    UILabel *labelName = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 50, 30)];
//    labelName.text = @"名字";
//    [self.view addSubview:labelName];
//    UILabel *labelAge = [[UILabel alloc]initWithFrame:CGRectMake(50, 150, 50, 30)];
//    labelAge.text = @"年龄";
//    [self.view addSubview:labelAge];
//    UITextField *txtID = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, 100, 40)];
//    [self.view addSubview:txtID];
//    UITextField *txtName = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, 100, 40)];
//    [self.view addSubview:txtName];
//    UITextField *txtAge = [[UITextField alloc]initWithFrame:CGRectMake(110, 50, 100, 40)];
//    [self.view addSubview:txtAge];
//    
//    UIButton *btnSave = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 50, 40)];
//    [self.view addSubview:btnSave];
//    
//    UIButton *btnRead = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 50, 40)];
//    [self.view addSubview:btnRead];
//    
//    UIButton *btnSave1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 50, 40)];
//    [self.view addSubview:btnSave1];
//    
//    UIButton *btnRead1 = [[UIButton alloc]initWithFrame:CGRectMake(200, 300, 50, 40)];
//    [self.view addSubview:btnRead1];
//
//    UIButton *btnSave2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 400, 50, 40)];
//    [self.view addSubview:btnSave2];
//    
//    UIButton *btnRead2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 400, 50, 40)];
//    [self.view addSubview:btnRead2];

    //得到沙盒的路径
    //方式1
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    //documents的目录
    NSString *docPath = paths[0];
    
    //方式2
    NSString *docPath2 = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
    
    
}
#define kStuId @"StuId"
#define kStuName @"StuName"
#define kStuAge @"StuAge"
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)UserDBtn:(id)sender {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:self.stuID.text forKey:kStuId];
    [user setObject:self.stuName.text forKey:kStuName];
    [user setObject:self.stuAge forKey:kStuAge];
    [user synchronize];
}
- (IBAction)UserDSave:(id)sender {
    
    
    
    
}
//存入的信息用数组或字典
- (IBAction)plistSave:(id)sender {
    NSDictionary *dic = @{@"stuId":self.stuID.text,
                          @"stuName":self.stuName.text,
                          @"stuAge":self.stuAge.text
                          };
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    NSLog(@"%@",filePath);
    [dic writeToFile:filePath atomically:YES];
    
}

- (IBAction)plistRead:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    self.stuID.text = dic[@"stuId"];
    self.stuName.text = dic[@"stuName"];
    self.stuAge.text = dic[@"stuAge"];
 
}
//归档方式（数据放入支持归档的对象）
- (IBAction)guidangSave:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    StudentModel *m = [[StudentModel alloc]initWithId:self.stuID.text andName:self.stuName.text andAge:self.stuAge.text];
    NSMutableArray *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mData];
    [arch encodeObject:m forKey:@"Student"];
    [arch finishEncoding];
}

- (IBAction)guidangRead:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    //读取数据（二进制数据）
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //创建解档对象
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    //从解档对象中解档Student
    StudentModel *stu = [unArch decodeObjectForKey:@"Student"];
    //完成解档
    [unArch finishDecoding];
    
    self.stuID.text = stu.stuId;
    self.stuName.text = stu.stuName;
    self.stuAge.text = stu.stuAge;
    
    
}

- (IBAction)click:(id)sender {
    [self.view endEditing:YES];
}
@end
